package com.blockchain.stockexchange.controller;

import com.blockchain.stockexchange.dto.ApiResponse;
import com.blockchain.stockexchange.security.UserPrincipal;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    
    @GetMapping("/me")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getCurrentUser(
            @AuthenticationPrincipal UserPrincipal userPrincipal) {
        
        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("id", userPrincipal.getId());
        userInfo.put("username", userPrincipal.getUsername());
        userInfo.put("email", userPrincipal.getEmail());
        userInfo.put("eoaAddress", userPrincipal.getEoaAddress());
        userInfo.put("role", userPrincipal.getAuthorities().iterator().next().getAuthority().replace("ROLE_", ""));
        
        return ResponseEntity.ok(ApiResponse.success(userInfo));
    }
}




